import Vue from 'vue';

const HOOK = {
  beforeSearch: 'beforeSearch',
  beforeApplyAuth: 'beforeApplyAuth',
  afterCloseApply: 'afterCloseApply'
};
const viewMixin = {
  provide() {
    return {
      view: this
    };
  },
  data() {
    return {
      dataTableComp: null
    }
  },
  methods: {
    mountEl(id = 'apply-auth') {
      const div = document.createElement('div');
      div.id = id;
      this.$el.appendChild(div);
    },
    search(searchForm) {
      if (!this.callHook(HOOK.beforeSearch, searchForm)) {
        return;
      }
      const dataTableComp = this.getDataTableComp();
      if (!dataTableComp) { return }
      // 设置数据表格请求参数
      // searchFormComp.$attrs.value
      dataTableComp.req.params = searchForm;
      //重新初始化表格
      dataTableComp.initTable();
    },
    getDataTableComp() {
      return this.dataTableComp || (this.dataTableComp = this.findComponentDown(this, 'DataTable'));
    },
    //懒加载数据表格侧面栏组件
    importTableSidebar() {
      return import('@/components/applySearch/index2.vue')
    },
    // 手动挂载TableSidebar组件
    createTableSidebar(moduleName, sourceId) {
      return this.importTableSidebar().then(component => {
        const TableSidebar = Vue.extend(component.default);
        const vm = new TableSidebar({
          propsData: {
            moduleName,
            sourceId
          }
        });
        vm.$mount('#apply-auth');
        vm.$on('closeDialog', () => {
          this.callHook(HOOK.afterCloseApply);
        });
        return vm;
      });
    },
    //申请查看功能
    async applyView(moduleName, sourceId) {
      let newRow = null;
      newRow = this.callHook(HOOK.beforeApplyAuth, moduleName, sourceId);
      if (!newRow) {
        return;
      } else {
        if (newRow != true) {
          if (newRow instanceof Promise) {
            row = await newRow;
          } else {
            row = newRow
          }
        }
      }
      this.mountEl();
      this.createTableSidebar(moduleName, sourceId);
    },
    //向上查找指定父组件
    findComponentUp(context, componentName) {
      let parent = context.$parent;
      let name = context.$options.name;
      while (parent && (!name || [componentName].indexOf(name) < 0)) {
        parent = parent.$parent;
        if (parent) {
          name = parent.$options.name;
        }
      }
    },
    // 向下查找指定子组件
    findComponentDown(context, componentName) {
      const childrens = context.$children;
      let children = null;
      if (childrens.length) {
        for (const child of childrens) {
          const name = child.$options.name;
          if (name === componentName) {
            children = child;
            break;
          } else {
            children = this.findComponentDown(child, componentName);
            if (children) {
              break;
            }
          }
        }
      }
      return children;
    },
    // 执行HOOK
    callHook(hook, ...args) {
      return this[hook] && this[hook](...args) || true;
    }
  }
};

export default viewMixin

export {
  HOOK
}